package com.qiao.glod.ui.fragment;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.qiao.glod.R;
import com.qiao.glod.adapter.FragmentItemPagerAdapter;
import com.qiao.glod.widget.smartlayout.SmartTabLayout;

/**
 * Created by Qiao on 2016/4/8.
 * function：
 */
public class TabFragment extends QFragment {
    protected ViewPager mViewPager;
    protected SmartTabLayout mTabLayout;

    @Override
    protected int generateResId() {
        return R.layout.fragment_tab;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mViewPager = findView(R.id.viewpager);
        mTabLayout = findView(R.id.tablayout);

        mViewPager.postDelayed(bindTabsRunnable,100);
    }

    private Runnable bindTabsRunnable = new Runnable() {
        @Override
        public void run() {
            mViewPager.setAdapter(getFragmentAdapter());
            mViewPager.setOffscreenPageLimit(mViewPager.getAdapter().getCount());
            mTabLayout.setViewPager(mViewPager);

            mViewPager.getAdapter().notifyDataSetChanged();
        }
    };

    protected FragmentItemPagerAdapter getFragmentAdapter(){
        return FragmentItemPagerAdapter.Builder.create(getActivity())
                .add("tab1", TFragment.class, getArguments())
                .add("tab2", TFragment.class, getArguments())
                .add("tab3", TFragment.class, getArguments())
                .add("tab4", TFragment.class, getArguments())
                .build(getChildFragmentManager());
    }

    public static class TabItem{
        String title;
        String fragmentClass;
        Bundle args;

        public TabItem(String title, String fragmentClass) {
            this.title = title;
            this.fragmentClass = fragmentClass;
        }

        public TabItem(String title, String fragmentClass, Bundle args) {
            this.title = title;
            this.fragmentClass = fragmentClass;
            this.args = args;
        }

        public String getTitle() {
            return title;
        }

        public String getFragmentClass() {
            return fragmentClass;
        }

        public Bundle getArgs() {
            return args;
        }
    }
}
